﻿Imports System.Reflection
Imports System.ComponentModel
Imports Ocean.OceanFramework.CommonDialog
Imports Ocean.OceanFramework.WPF.Utility

Namespace MVVM

    Public MustInherit Class ContainerFormViewModelBase
        Inherits ClosableViewModelBase

#Region " Properties "

        Public MustOverride ReadOnly Property IsDirty() As Boolean

        ''' <summary>
        ''' Allows application to determine if this Container Form is a Single Instance form.
        ''' This property should be checked before opening up a second instance of this form.
        ''' This property defaults to True and should be overriden in deriving classes as required.
        ''' </summary>
        Public Overridable ReadOnly Property IsSingleInstanceForm() As Boolean
            Get
                Return True
            End Get
        End Property

#End Region

#Region " Constructor "

        Public Sub New()
        End Sub

#End Region

#Region " Methods "

        Public MustOverride Function SaveExecute() As Integer

        Public Overridable Function VerifyDataBeforeClosing() As Boolean

            If Not IsDirty Then
                Return True
            End If

            Dim enumResult As CustomDialogResult = ViewModelUIService.YesNoCancelConfirmSave

            If enumResult = CustomDialogResult.Cancel Then
                Return False

            ElseIf enumResult = CustomDialogResult.No Then
                Return True

            Else

                Dim intResult As Integer = SaveExecute()

                If intResult <> 0 Then
                    Return True

                Else
                    Return False
                End If

            End If

        End Function

        Protected Overrides Sub OnRequestClose()

            If VerifyDataBeforeClosing() Then
                MyBase.OnRequestClose()
            End If

        End Sub

#End Region

    End Class

End Namespace
